package com.jt.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CartService {
   @Autowired
   private RedisTemplate redisTemplate;
   //添加购物车 [没有返回值]
   public void addCart(String userId,String productId,Integer num){
        HashOperations hash = redisTemplate.opsForHash();
        hash.increment(userId, productId, num);
    }
    //查看购物车 【有返回值】
  public Map<String,Object> getCart(String userId){
       HashOperations hash = redisTemplate.opsForHash();
       return hash.entries(userId);
    }
   //
   public List<Object> listCart2(Integer userId){
       HashOperations hashOperations = redisTemplate.opsForHash();
       return hashOperations.values("cart:"+userId);
   }
}
